Ruby - overriding/enabling multiple assignment (e.g. `a, b, c = d, e, f`)
Posted
by
nicholaides
on Stack Overflow
See other posts from Stack Overflow
or by nicholaides
Published on 2010-12-28T02:53:43Z
Indexed on
2010/12/28
3:54 UTC
Read the original article
Hit count: 122
ruby
In ruby, you can do this:
d = [1, 2, 3]
a, b, c = d
a
, b
, and c
, will receive the values 1, 2, and 3, respectively.
d
, in this case in an Array
and ruby knows to assign it's contents to a
, b
, and c
. But, if d
were a Fixnum
, for example, only a
would be assigned to the value of d
while b
and c
would be assigned nil
.
What properties of d
allow it to be used for multiple assignment? In my exploring so far, I've only been able to make instances of subclasses of Array
behave in this way.
© Stack Overflow or respective owner